﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.PM.Shell.Dialogs
{
    /// <summary>
    /// Interaction logic for WorkStageFilterDlg.xaml
    /// </summary>
    public partial class WorkStageFilterDlg : Window
    {
        public WorkStageFilterDlg(WorkSheet workSheet)
        {
            InitializeComponent();

            this.Title = "阶段过滤";

            BindToTreeView(workSheet);
        }

        /// <summary>
        /// 绑定工作表的阶段到treeview上
        /// </summary>
        /// <param name="workSheet"></param>
        private void BindToTreeView(WorkSheet workSheet)
        {
            foreach (WorkStageRef workStageRef in workSheet.WorkStageCollection)
            {
                TreeViewItem treeViewItem = new TreeViewItem();

                CheckBox ctlCheckBox = new CheckBox();
                ctlCheckBox.DataContext = workStageRef;
                ctlCheckBox.SetBinding(CheckBox.ContentProperty, new Binding("Caption"));
                if (workStageRef.IsDisplay)
                {
                    ctlCheckBox.IsChecked = true;
                }

                treeViewItem.Header = ctlCheckBox;
                treeViewItem.IsExpanded = true;

                _ctlTreeViewWorkStage.Items.Add(treeViewItem);

                BindToTreeViewItem(workStageRef, treeViewItem);
            }
        }

        private void BindToTreeViewItem(WorkStageRef workStageRef, TreeViewItem parentTreeViewItem)
        {
            foreach (WorkStageItemRef workStageItemRef in workStageRef.ItemCollection)
            {
                TreeViewItem treeViewItem = new TreeViewItem();

                CheckBox ctlCheckBox = new CheckBox();
                ctlCheckBox.DataContext = workStageItemRef;
                ctlCheckBox.SetBinding(CheckBox.ContentProperty, new Binding("Caption"));
                if (workStageItemRef.IsDisplay)
                {
                    ctlCheckBox.IsChecked = true;
                }

                treeViewItem.Header = ctlCheckBox;

                parentTreeViewItem.Items.Add(treeViewItem);
            }
        }

        /// <summary>
        /// 从treeview上更新选中状态到model中
        /// </summary>
        private void UpdateFromTreeView()
        {
            for (int index = 0; index < _ctlTreeViewWorkStage.Items.Count; index++)
            {
                TreeViewItem treeViewItem = _ctlTreeViewWorkStage.Items[index] as TreeViewItem;
                CheckBox ctlCheckBox = treeViewItem.Header as CheckBox;
                WorkStageRef workStageRef = ctlCheckBox.DataContext as WorkStageRef;
                if (workStageRef != null)
                {
                    if (ctlCheckBox.IsChecked == true)
                    {
                        workStageRef.IsDisplay = true;
                    }
                    else
                    {
                        workStageRef.IsDisplay = false;
                    }
                }
                else
                {
                    WorkStageItemRef workStageItemRef = ctlCheckBox.DataContext as WorkStageItemRef;
                    if (ctlCheckBox.IsChecked == true)
                    {
                        workStageItemRef.IsDisplay = true;
                    }
                    else
                    {
                        workStageItemRef.IsDisplay = false;
                    }
                }

                UpdateFromTreeViewItem(treeViewItem);
            }
        }

        private void UpdateFromTreeViewItem(TreeViewItem parentTreeViewItem)
        {
            for (int index = 0; index < parentTreeViewItem.Items.Count; index++)
            {
                TreeViewItem treeViewItem = parentTreeViewItem.Items[index] as TreeViewItem;
                CheckBox ctlCheckBox = treeViewItem.Header as CheckBox;
                WorkStageRef workStageRef = ctlCheckBox.DataContext as WorkStageRef;
                if (workStageRef != null)
                {
                    if (ctlCheckBox.IsChecked == true)
                    {
                        workStageRef.IsDisplay = true;
                    }
                    else
                    {
                        workStageRef.IsDisplay = false;
                    }
                }
                else
                {
                    WorkStageItemRef workStageItemRef = ctlCheckBox.DataContext as WorkStageItemRef;
                    if (ctlCheckBox.IsChecked == true)
                    {
                        workStageItemRef.IsDisplay = true;
                    }
                    else
                    {
                        workStageItemRef.IsDisplay = false;
                    }
                }

                UpdateFromTreeViewItem(treeViewItem);
            }
        }

        private void OnCtlBtnOKClick(object sender, RoutedEventArgs e)
        {
            UpdateFromTreeView();
            DialogResult = true;
        }
    }
}
